﻿package syerit.core.ui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.SimpleButton;
	import flash.display.MovieClip;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.system.fscommand;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	
	/**
	 * 
	 * @author Pavel Jacko syerit.com
	 * 
	 */	
	public class ColorButton extends Sprite
	{
		public var index:int;
		private var image:Bitmap;
		private var trigger:Function;
		private var triggerParams:Array;
		private var defColor:ColorTransform;
		private var newColor:ColorTransform;
		private var colors:Array;
		private var xVal:Number;
		private var yVal:Number;
		
		//private var isSelected:Boolean = false;
		
		public function ColorButton(image:Bitmap,x:Number=0,y:Number=0,colors:Array=null,trigger:Function=null,triggerParams:Array=null,index:int=0)
		{
			this.xVal=x;
			this.yVal=y;
			this.image=image;
			this.trigger=trigger;
			this.triggerParams=triggerParams;
			this.index=index;
			this.colors=colors;
			
			super();
			init();
		}
		
		
		
		private function init():void
		{
			x=xVal;
			y=yVal;
			trace("ImageButton"+image+" created.");
			var bmd:BitmapData = new BitmapData(image.width,image.height,true,0);
			bmd.draw(image);
			var bm:Bitmap = new Bitmap(bmd);
			addChild(bm);
			
			buttonMode=true;
			
			activate();
			defColor  =  this.transform.colorTransform;
			newColor  =  this.transform.colorTransform;
			setColor(0);
		}
		
		private function onOver(e:MouseEvent):void
		{
			setColor(1);
			//this.alpha=.5;
		}
		
		private function onOut(e:MouseEvent):void
		{
			setColor(0);
			//this.alpha=1;
		}
		
		private function onClick(e:MouseEvent):void
		{
			setColor(2);
			//trace("HAAAAAAA"+triggerParams);
			trigger.apply(this,triggerParams);
			//isSelected=true;
		}
		
		public function select():void
		{
			setColor(2);
			//isSelected=true;
		}
		
		public function release():void
		{
			//isSelected=false;
			setColor(0);
		}
		
		public function press():void
		{
			//isSelected=true;
			setColor(1);
		}
		
		private function setColor(col:int):void
		{
			if(colors!=null)
			{
				if(colors[col]!=null)
				{
					//trace("color to"+col);
					newColor.color = colors[col];
					this.transform.colorTransform = newColor;
				}
			}
		}
		
		public function activate():void
		{
			if(trigger!=null)
			{
				addEventListener(MouseEvent.CLICK,onClick);
			}
			addEventListener(MouseEvent.MOUSE_OVER,onOver);
			addEventListener(MouseEvent.MOUSE_OUT,onOut);
		}
		
		public function deactivate():void
		{
			removeEventListener(MouseEvent.MOUSE_OVER,onOver);
			removeEventListener(MouseEvent.MOUSE_OUT,onOut);
			removeEventListener(MouseEvent.CLICK,onClick);
		}
	}
}